import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/** ServletApplet shows
   -- Servlet and Applet communication.
   -- The servlet can provides text and binary data (not necessary HTML)
      to applet.
   -- The Applet use URLConnection (for POST method) or URL methods to
      connect to servlet.
*/

public class ServletApplet extends HttpServlet {

   public void doGet( HttpServletRequest req, HttpServletResponse res )
	throws ServletException, IOException 
   {

	String par = req.getParameter("type");
	if ( par.equals("Date") ) { // send plain text out.
		PrintWriter out;
		// res.setContentType("text/plain");
		Date dt = new Date();
		out = res.getWriter();
		out.println(dt.toString());
		out.close();
	} else {		// send binary data
		res.setContentType("*/*");
		OutputStream out = res.getOutputStream();
		// ObjectOutputStream oout = new ObjectOutputStream(out);
		// oout.writeObject( new Integer( (int)( Math.random() * 1000)) );
		out.write( (int) ( Math.random() * 1000) );
		out.close();
		// oout.close();
	}
   }

}
